function vns2ascii(inDir,outDir,outAsciiDir)

%inDir='C:\Users\matthew\Desktop\workspace\vnsoddmc\data\vns\';
%outDir='C:\Users\matthew\Desktop\workspace\vnsoddmc\analysis\vns\';
%outAsciiDir='C:\Users\matthew\Desktop\workspace\vnsoddmc\analysis\vns\ascii\';


filter=fullfile(inDir,'*_*');
subjs=dir(filter);
nSubjs=length(subjs);

bandNames={'theta','alpha','beta','gamma'};
bands={[4 8],[8 13],[13 22],[22 49]};
nBands=length(bands);
trialLen=1024;
precision='%.20f';
nTrials2use=0;
subjsNames={};
minNTrials=zeros(1,nSubjs)
for i=1:nSubjs
    subjsNames{i}=subjs(i).name
    filename=fullfile(inDir,subjs(i).name);
    data=importEDF2FT(filename);
    trials=rechunk(data,trialLen);
    
   
    minNTrials(i)=length(trials.trial);
end
nTrials2use=min(minNTrials);

for i=1:nSubjs
    filename=fullfile(inDir,subjs(i).name);
    data=importEDF2FT(filename);
    trials=rechunk(data,trialLen);
    
    for j=1:nBands
        [bf_trials]=selectBand(trials,bands{j});
        
        [outfile,r]=strtok(subjs(i).name,'.');
        
        outfile=strcat('_',outfile);
        outfile=strcat(bandNames{j},outfile);
        outfile=strcat(outfile,'-trials');
          
        outAscii=strtok(outfile,'-');
        outAscii=strcat(outAscii,'-bfTrials.txt');
        write2ascii(fullfile(outAsciiDir,outAscii),bf_trials,nTrials2use,precision)
        
        
        %save(fullfile(outDir,outfile),'trials','bf_trials')
    end
    [r,outfile]=strtok(outfile,'_');
    outfile=outfile(2:end);
    write2ascii(fullfile(outAsciiDir,strcat(outfile,'.txt')),trials,nTrials2use,precision);
end


% function write2ascii(filename,trials,nTrials,precision)
% %write channel in column
% %nTrials=length(trials.trial);
% if(length(trials.trial)>= nTrials)
%     dlmwrite(filename, trials.trial{1}','delimiter','\t','precision',precision);%,'precision','%d'
% 
%     for i=2:nTrials
%         dlmwrite(filename,trials.trial{i}','-append','delimiter','\t','precision',precision);
%     end
% else
%     warning=strcat(filename,'--not enough trials')
% end

function [trials]=rechunk(data,nSamples)
cfg=[];

len=nSamples/data.fsample;
cfg.length=len;  
cfg.overlap=0;
trials=ft_redefinetrial(cfg,data);
function [bf_trials]=selectBand(trials,band)

bf_trials=trials;
nTrials=length(trials.trial);

for i=1:nTrials
    
    bf_trials.trial{i}=eegfilt(trials.trial{i},trials.fsample,band(1),band(2));

end
function [cdata]=importEDF2FT(filename)

chLabelslOI={'MKR+','Fp1','Fp2','F7','F3','Fz','F4','F8','T3','C3','Cz','C4','T4','T5','P3','Pz','P4','T6','O1','O2'}
%filename='C:\Users\matthew\Desktop\Projects\VNS\data\Autiero 5.05.08 7y.edf';
%channel of interest
to_import=pop_biosig(filename);
chLabels={to_import.chanlocs.labels};
choi=zeros(1,length(chLabels));
for i=1:length(chLabelslOI)
    choi=choi+strcmp(chLabelslOI{i},chLabels);
end
indxChOI=1:length(chLabels);
choi=logical(choi);
data.trial{1}=to_import.data(indxChOI(choi),:);
indxMKR=strcmp('MKR+',chLabels);
choi(indxMKR)=0;%mk+
data.label={to_import.chanlocs(indxChOI(choi)).labels}';



data.label

data.fsample=to_import.srate;

cdata=chunkData(data);
function [cdata]=chunkData(data)

cdata=data;   
mk=data.trial{1}(1,:);
mk=abs(mk);
indx=mk>1;
indxv=1:length(indx);
v=indxv(indx);
indx=splitVec(v,'consecutive');

[s ntrial]=size(indx);
cdata.trial=cell(s,ntrial);
sampl_interval=1/cdata.fsample;

for i=1:ntrial
    cdata.trial{i}=data.trial{1}(2:end,indx{i});
    cdata.time{i}=(0:1:length(cdata.trial{i})-1)*sampl_interval;
end


